<?php
session_start(); 
include 'phpfunctions.php';
include('data/lib_f_pictures.php');
include('data/lib_f_validate.php');

$db = getdb();
$table = "items";
if ($_SESSION['admin'])
{
	$edit = '1';
}
if($_POST['Submit'])
{
	echo 'Updating details... ';
	$target_path = "images/items/";
	if($_FILES['picture']['name'] && uploadPicture($_FILES['picture'],$target_path,$validpictypes,$maximgheight,$maximgwidth,$maximgsize,$picmsg))
	{
		$picture = $_FILES['picture']['name'];
	} else {
		$err .= $picmsg;
	}
	
	extract($_POST);
	if($description && !$description = validateText($description)){$err .= 'Description is invalid!<br>';}
	if($weight && !$weight = validateText($weight)){$err .= 'Weight is invalid!<br>';}
	if($size && !$size = validateText($size)){$err .= 'Size is invalid!<br>';}
	if(!$err)
	{
		$query = 'UPDATE items SET description="'.$description.'", weight="'.$weight.'", size="'.$size.'"' . ($picture ? ', picture="'.$picture.'"':'') .' WHERE id="'.$id.'"';
		$result = mysql_query($query);
	} else {
		echo $err;
	} 

}
if(!$id)$id=$_GET['id'];

$query = "SELECT * FROM " . $table . " WHERE id = '$id'";
$result = mysql_query($query);
dm($query . " : "  . $result . "<br>");
$item = mysql_result($result,0,"item_name");
$cost_day = mysql_result($result,0,'day_price');
$cost_track= mysql_result($result,0,'trip_price');
$description = mysql_result($result,0,'description');
$weight = mysql_result($result,0,'weight');
$picture = mysql_result($result,0,'picture');
$size = mysql_result($result,0,'size');
mysql_close();
//$brands = getBrandsFromIDs(mysql_result($result,0,'brand_ids'));

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Bev's Tramping Gear Hire - Item: <?php echo $item ?></title>
<link href="template.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="container">
	<div id="header">	</div>
		<div id="nav">
    <?php include 'navbar.php';?>
    </div>
<div id="content">
  <div class="style1" id="item">
    <p><a href="pricelist.php">Price List</a> &gt;&gt; Item: <?php echo "<h1>".$item."</h1>"; echo ($edit)?'<div align="center"><form action="login.html" method="POST" enctype="multipart/form-data"><input type="submit" name="logout" id="logout" value="Logout" /></div></form>':' ';  ?></p>
    <table width="100%" height="328" border="0" align="left" class="style1">
      <tr>
        <td width="20%"><p><?php 
		if($edit)
		{
			echo '<form action="'.$_SERVER['PHP_SELF'].'?id='.$id.'" method="POST" enctype="multipart/form-data">
			<input type="hidden" name="id" id="id" value="'.$id.'">
			<label>Upload New Picture <input type="file" name="picture" id="picture" size="45" maxlength="255"></label>';
		} 
		if($picture)
		{
			echo getPic('images/items/'.$picture,false,false,$item);
		} else {
			echo 'No Pic Sorry!';
		}?></p></td>
        <td width="80%"><table width="100%" border="0">
            <tr>
              <td width="11%"><span class="heading">Pricing:</span></td>
              <td width="42%"><span class="style1">Day Charge:</span> $<?php echo $cost_day ?>.00</td>
              <td width="47%"><span class="style1">Track Charge:</span> $<?php echo $cost_track ?>.00</td>
            </tr>
          </table>
            <p><?php echo (($edit)?'<textarea name="description" rows="20" cols="70">'.$description.'</textarea>':$description); ?>
            <p>
            <p>            
            <p>            
            <p>            
            <p>            
            <p>            
            <p>            
            <p>            
            <p>            
            <p>
                                    <tr><td colspan="2"><table width="100%" border="0" cellspacing="2" cellpadding="2">
              <tr>
                <td><span class="style1">Estimated Weight:</span> <?php echo(($edit)? '<input name="weight" type="text" id="weight" value="'.$weight.'" />' : $weight); ?> </td>
                <td><span class="style1">Estimated Size:</span> <?php echo ($edit)?'<input name="size" type="text" id="size" value="'.$size.'" />':$size ?> </td>
              </tr>
            </table></td>
      </tr>
      <tr>
        <td colspan="2"> <?php echo ($edit)?'<input type="submit" name="Submit" id="Submit" class="button" value="Submit"></form>':"";?></td>
      </tr>
    </table>
    <p>&nbsp;</p>

    <p>&nbsp;</p>
  </div>
  <div align="center" class="style1" id="footer">
      <p><strong><a href="http://www.fiordland.org.nz/">Visit Destination Fiordland for all your information on Fiordland and Te Anau<br/>
      </a></strong></p>
      2007 webdesign by <a href="http://justcreations.co.nz">Justcreations</a> &amp; Brian Thorne, All rights reserved, Any technical problems email the <?php echo email_link('Brian', 'webmaster', 'Email Brian the webmaster','Enquiry from bevs-hire website'); ?>  </div>
</div>
</div>
</body>
</html>